package org.viva.service.wx.qy;

import org.viva.api.HTTPUtils;
import org.viva.entity.wx.Token;

import net.sf.json.JSONObject;

public class QYAPI {
    public static final String access_token_url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=CorpID&corpsecret=SECRET";
    public static String       menu_create_url  = "https://qyapi.weixin.qq.com/cgi-bin/menu/create?access_token=ACCESS_TOKEN&agentid=1";

    public static Token getAccessToken(String corpID, String secret) {
        Token accessToken = null;
        String requestUrl = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=CorpID&corpsecret=SECRET".replace("CorpID", corpID).replace("SECRET",
                secret);
        JSONObject jsonObject = JSONObject.fromObject(HTTPUtils.doGet(requestUrl));

        if (null != jsonObject) {
            try {
                accessToken = new Token();
                accessToken.setToken(jsonObject.getString("access_token"));
                accessToken.setExpiresIn(jsonObject.getInt("expires_in"));
                System.out.println("获取token成功:" + jsonObject.getString("access_token") + "————" + jsonObject.getInt("expires_in"));
            } catch (Exception e) {
                accessToken = null;
                String error = String.format("获取token失败 errcode:{} errmsg:{}",
                        new Object[] { Integer.valueOf(jsonObject.getInt("errcode")), jsonObject.getString("errmsg") });
                System.out.println(error);
            }
        }
        return accessToken;
    }

    // public static int createMenu(Menu menu, String accessToken) {
    // int result = 0;
    //
    // String url = menu_create_url.replace("ACCESS_TOKEN", accessToken);
    //
    // String jsonMenu = JSONObject.fromObject(menu).toString();
    //
    // JSONObject jsonObject = HttpRequest(url, "POST", jsonMenu);
    //
    // if ((null != jsonObject) && (0 != jsonObject.getInt("errcode"))) {
    // result = jsonObject.getInt("errcode");
    // String error = String.format("创建菜单失败 errcode:{} errmsg:{}",
    // new Object[] { Integer.valueOf(jsonObject.getInt("errcode")),
    // jsonObject.getString("errmsg") });
    // System.out.println(error);
    // }
    //
    // return result;
    // }
    //
    // public static String URLEncoder(String str) {
    // String result = str;
    // try {
    // result = URLEncoder.encode(result, "UTF-8");
    // } catch (Exception e) {
    // e.printStackTrace();
    // }
    // return result;
    // }
    //
    // public static String getFileEndWitsh(String contentType) {
    // String fileEndWitsh = "";
    // if ("image/jpeg".equals(contentType)) fileEndWitsh = ".jpg";
    // else if ("audio/mpeg".equals(contentType)) fileEndWitsh = ".mp3";
    // else if ("audio/amr".equals(contentType)) fileEndWitsh = ".amr";
    // else if ("video/mp4".equals(contentType)) fileEndWitsh = ".mp4";
    // else if ("video/mpeg4".equals(contentType)) fileEndWitsh = ".mp4";
    // return fileEndWitsh;
    // }
    //
    // public static JSONObject PostMessage(String access_token, String
    // RequestMt, String RequestURL, String outstr) {
    // int result = 0;
    // RequestURL = RequestURL.replace("ACCESS_TOKEN", access_token);
    // JSONObject jsonobject = HttpRequest(RequestURL, RequestMt, outstr);
    // if ((null != jsonobject) && (0 != jsonobject.getInt("errcode"))) {
    // result = jsonobject.getInt("errcode");
    // }
    // return jsonobject;
    // }

}
